﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Media.Imaging;
using System.Drawing.Imaging;
using System.IO;

namespace Shoozla.DataModel.Helper
{

    public static class StringHelper
    {

        private const Char Separator = '|';

        public static String ConcatenateNames(String name1, String name2)
        {
            return name1 + Separator + name2;
        }

        public static String[] GetSeparatedNames(String concatenatedName)
        {
            return concatenatedName.Split(Separator);
        }

        public static String PrepareStringForWeb(String s)
        {
            //invalid char from RFC 2396 (page 10)

            String safe = s;
            s = s.Replace("[", "");
            s = s.Replace("]", "");
            s = s.Replace("{", "");
            s = s.Replace("}", "");
            s = s.Replace("\\", "");
            s = s.Replace("|", "");
            s = s.Replace("^", "");
            s = s.Replace("`", "");

            return s;
        }
    }

    public enum ThreadingFactorEnum
    {
        Single,
        Medium,
        Maximum
    }

    public static class ThreadHelper
    {
        //    public static int CalculateTreads(int itemsCount, ThreadingFactorEnum threadingFactor)
        //    {
        //        switch (threadingFactor)
        //        {
        //            case ThreadingFactorEnum.Single:
        //                {
        //                    //== num of cpu
        //                    System.Management.ManagementObject mo = new System.Management.ManagementObject("Win32_ComputerSystem.Name=\"" + Environment.MachineName + "\"");
        //                    return Int32.Parse(mo["NumberOfLogicalProcessors"].ToString());
        //                }
        //            case ThreadingFactorEnum.Medium:
        //                {
        //                    int n = (itemsCount / 4);
        //                    if (n <= 0) return 1;
        //                    else
        //                        return n;
        //                };
        //            case ThreadingFactorEnum.Maximum: { return itemsCount>100?100:itemsCount; } //max 100 threads
        //            default: { return 10; }
        //        }
        //    }
        //}

        public static int CalculateTreads(int itemsCount, ThreadingFactorEnum threadingFactor)
        {
            switch (threadingFactor)
            {
                case ThreadingFactorEnum.Single:
                    {
                        //== num of cpu
                        System.Management.ManagementObject mo = new System.Management.ManagementObject("Win32_ComputerSystem.Name=\"" + Environment.MachineName + "\"");
                        return Int32.Parse(mo["NumberOfLogicalProcessors"].ToString());
                    }

                default: { return -1; } //-1 means no limit! --> faster
            }
        }
    }

    public static class ImageHelper
    {
        private static object _o = new Object();

        public static BitmapImage ImageToBitmapImage(Image im)
        {
            if (im == null)
                return new BitmapImage();
            //lock (_o)
            //{
                try
                {
                    using (Bitmap tempIm = new Bitmap(im))
                    {
                        using (MemoryStream ms = new MemoryStream())
                        {
                            tempIm.Save(ms, im.RawFormat);
                            BitmapImage bImg = new System.Windows.Media.Imaging.BitmapImage();

                            bImg.BeginInit();
                            bImg.StreamSource = new MemoryStream(ms.ToArray()); //avoiding GDI errors
                            bImg.CreateOptions = BitmapCreateOptions.None;
                            bImg.CacheOption = BitmapCacheOption.Default;
                            bImg.EndInit();

                            ms.Close();
                            return bImg;
                        }
                    }
                }
                catch(Exception imageException)
                {
                    return null; //do not trigger any errors 
                }
            //}

            //if (im == null)
            //    return null;
           
            //    try
            //    {
            //        Image tempIm = (Image)im.Clone();  
            //        MemoryStream ms = new MemoryStream();
            //        //ImageCodecInfo jgpEncoder = GetEncoder(im.RawFormat);
            //        //System.Drawing.Imaging.Encoder myEncoder = System.Drawing.Imaging.Encoder.Quality;
            //        //EncoderParameters myEncoderParameters = new EncoderParameters(1);
            //        //EncoderParameter myEncoderParameter = new EncoderParameter(myEncoder, 100L);
            //        //myEncoderParameters.Param[0] = myEncoderParameter;
            //        //im.Save(ms, jgpEncoder, myEncoderParameters);
            //        tempIm.Save(ms, tempIm.RawFormat);
            //        BitmapImage bi = new BitmapImage();
            //        bi.BeginInit();
            //        bi.StreamSource = new MemoryStream(ms.ToArray());
            //        bi.CreateOptions = BitmapCreateOptions.None;
            //        bi.CacheOption = BitmapCacheOption.Default;
            //        bi.EndInit();
            //        return bi;
            //    }
            //    catch(Exception imageException)
            //    {
            //        return null; //do not trigger any errors 
            //    }
                    
           
        }

        private static ImageCodecInfo GetEncoder(ImageFormat format)
        {

            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.FormatID == format.Guid)
                {
                    return codec;
                }
            }
            return null;
        }

    }
}
